home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / winterp-1.13 / examples / xlisp-1.6 / object.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1991-10-06  |  3.6 KB  |  93 lines

  1. ; -*-Lisp-*-
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;
  4. ; File:         object.lsp
  5. ; RCS:          $Header: $
  6. ; Description:  This is an example using the object-oriented programming support
  7. ;        in XLISP.  The example involves defining a class of objects
  8. ;        representing dictionaries.  Each instance of this class will be
  9. ;        a dictionary in which names and values can be stored.  There will
  10. ;        also be a facility for finding the values associated with names
  11. ;        after they have been stored.
  12. ; Author:       ???
  13. ; Created:      Sat Oct  5 20:56:06 1991
  14. ; Modified:     Sat Oct  5 20:57:10 1991 (Niels Mayer) mayer@hplnpm
  15. ; Language:     Lisp
  16. ; Package:      N/A
  17. ; Status:       X11r5 contrib tape release
  18. ;
  19. ; WINTERP Copyright 1989, 1990, 1991 Hewlett-Packard Company (by Niels Mayer).
  20. ; XLISP version 2.1, Copyright (c) 1989, by David Betz.
  21. ;
  22. ; Permission to use, copy, modify, distribute, and sell this software and its
  23. ; documentation for any purpose is hereby granted without fee, provided that
  24. ; the above copyright notice appear in all copies and that both that
  25. ; copyright notice and this permission notice appear in supporting
  26. ; documentation, and that the name of Hewlett-Packard and Niels Mayer not be
  27. ; used in advertising or publicity pertaining to distribution of the software
  28. ; without specific, written prior permission.  Hewlett-Packard and Niels Mayer
  29. ; makes no representations about the suitability of this software for any
  30. ; purpose.  It is provided "as is" without express or implied warranty.
  31. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  32.  
  33. ; Create the 'Dictionary' class.
  34.  
  35. (setq Dictionary (Class 'new))
  36.  
  37. ; Establish the instance variables for the new class.
  38. ; The variable 'entries' will point to an association list representing the
  39. ; entries in the dictionary instance.
  40.  
  41. (Dictionary 'ivars '(entries))
  42.  
  43. ; Setup the method for the 'isnew' initialization message.
  44. ; This message will be send whenever a new instance of the 'Dictionary'
  45. ; class is created.  Its purpose is to allow the new instance to be
  46. ; initialized before any other messages are sent to it.  It sets the value
  47. ; of 'entries' to nil to indicate that the dictionary is empty.
  48.  
  49. (Dictionary 'answer 'isnew '()
  50.         '((setq entries nil)
  51.           self))
  52.  
  53. ; Define the message 'add' to make a new entry in the dictionary.  This
  54. ; message takes two arguments.  The argument 'name' specifies the name
  55. ; of the new entry; the argument 'value' specifies the value to be
  56. ; associated with that name.
  57.  
  58. (Dictionary 'answer 'add '(name value)
  59.         '((setq entries
  60.                 (cons (cons name value) entries))
  61.           value))
  62.  
  63. ; Create an instance of the 'Dictionary' class.  This instance is an empty
  64. ; dictionary to which words may be added.
  65.  
  66. (setq d (Dictionary 'new))
  67.  
  68. ; Add some entries to the new dictionary.
  69.  
  70. (d 'add 'mozart 'composer)
  71. (d 'add 'winston 'computer-scientist)
  72.  
  73. ; Define a message to find entries in a dictionary.  This message takes
  74. ; one argument 'name' which specifies the name of the entry for which to
  75. ; search.  It returns the value associated with the entry if one is
  76. ; present in the dictionary.  Otherwise, it returns nil.
  77.  
  78. (Dictionary 'answer 'find '(name &aux entry)
  79.         '((cond ((setq entry (assoc name entries))
  80.           (cdr entry))
  81.          (t
  82.           nil))))
  83.  
  84. ; Try to find some entries in the dictionary we created.
  85.  
  86. (d 'find 'mozart)
  87. (d 'find 'winston)
  88. (d 'find 'bozo)
  89.  
  90. ; The names 'mozart' and 'winston' are found in the dictionary so their
  91. ; values 'composer' and 'computer-scientist' are returned.  The name 'bozo'
  92. ; is not found so nil is returned in this case.
  93.